// home_script.js

document.addEventListener("DOMContentLoaded", () => {
    // 初始化节点和边数据
    const nodes = new vis.DataSet([
        { id: 1, label: "自然人", expanded: false },
        { id: 2, label: "个人市场", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },
        { id: 3, label: "家庭市场", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },
        { id: 4, label: "政企市场", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },
        { id: 5, label: "新兴市场", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },
        { id: 6, label: "上网行为", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },
        { id: 7, label: "位置信息", parentId: 1, expanded: true, hidden: false, color: getRandomColor() },

        { id: 201, label: "基础画像", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 202, label: "终端", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 203, label: "资费", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 204, label: "权益", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 205, label: "语音话单", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 206, label: "漫游", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },
        { id: 207, label: "流量话单", parentId: 2, expanded: true, hidden: false, color: getRandomColor() },

        { id: 20201, label: "终端销售", parentId: 202, expanded: true, hidden: false, color: getRandomColor() },
        { id: 20202, label: "终端使用", parentId: 202, expanded: true, hidden: false, color: getRandomColor() },
        { id: 2020201, label: "5G终端", parentId: 20202, expanded: true, hidden: false, color: getRandomColor() },

        { id: 2020202, label: "终端使用日明细表", parentId: 20202, expanded: true, hidden: false, color: getRandomColor() },
        { id: 202020201, label: "TAC码", parentId: 2020202, expanded: true, hidden: false, color: getRandomColor() },
        { id: 202020202, label: "网络制式", parentId: 2020202, expanded: true, hidden: false, color: getRandomColor() },

        { id: 20301, label: "5G套餐", parentId: 203, expanded: true, hidden: false, color: getRandomColor() },
        { id: 20302, label: "叠加包", parentId: 203, expanded: true, hidden: false, color: getRandomColor() },
        { id: 2030201, label: "流量统付叠加包订购信息表", parentId: 20302, expanded: true, hidden: false, color: getRandomColor() },
        { id: 203020101, label: "产品订单编码", parentId: 2030201, expanded: true, hidden: false, color: getRandomColor() },
        { id: 203020102, label: "成员号码", parentId: 2030201, expanded: true, hidden: false, color: getRandomColor() },
        { id: 203020103, label: "叠加套餐 ", parentId: 2030201, expanded: true, hidden: false, color: getRandomColor() },

        { id: 301, label: "家庭宽带", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 302, label: "魔百和", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 303, label: "家庭安防", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 304, label: "智能组网", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 305, label: "智慧社区", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 306, label: "数字乡村", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },
        { id: 307, label: "家庭IoT", parentId: 3, expanded: true, hidden: false, color: getRandomColor() },

        { id: 30201, label: "装维", parentId: 302, expanded: true, hidden: false, color: getRandomColor() },
        { id: 30202, label: "办理", parentId: 302, expanded: true, hidden: false, color: getRandomColor() },
        { id: 30203, label: "内容播放", parentId: 302, expanded: true, hidden: false, color: getRandomColor() },
        { id: 3020201, label: "场景化宽带订购明细表", parentId: 30202, expanded: true, hidden: false, color: getRandomColor() },
        { id: 302020101, label: "场景标识", parentId: 3020201, expanded: true, hidden: false, color: getRandomColor() },
        { id: 302020102, label: "是否到达用户", parentId: 3020201, expanded: true, hidden: false, color: getRandomColor() },


        { id: 401, label: "集团客户", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 402, label: "物联网", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 403, label: "有效融合", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 404, label: "企业宽带", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 405, label: "固话", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 406, label: "行业网关", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },
        { id: 407, label: "企业邮箱", parentId: 4, expanded: true, hidden: false, color: getRandomColor() },

        { id: 501, label: "咪咕视频", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 502, label: "移动云盘", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 503, label: "和包支付", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 504, label: "139邮箱", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 505, label: "中国移动APP", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 506, label: "5G新通话", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },
        { id: 507, label: "移动高清", parentId: 5, expanded: true, hidden: false, color: getRandomColor() },

        { id: 601, label: "DPI信息", parentId: 6, expanded: true, hidden: false, color: getRandomColor() },
        { id: 602, label: "DPI信息", parentId: 6, expanded: true, hidden: false, color: getRandomColor() },

        { id: 701, label: "漫游", parentId: 7, expanded: true, hidden: false, color: getRandomColor() },
        { id: 702, label: "位置信息", parentId: 7, expanded: true, hidden: false, color: getRandomColor() },
        { id: 703, label: "位置信息", parentId: 7, expanded: true, hidden: false, color: getRandomColor() },

        { id: 901, label: "话单", expanded: true, hidden: false, color: getRandomColor() },
        { id: 902, label: "5G", expanded: true, hidden: false, color: getRandomColor() },
        { id: 903, label: "宽带", expanded: true, hidden: false, color: getRandomColor() },
    ]);

    const edges = new vis.DataSet([
        { from: 1, to: 2, label: "" },
        { from: 1, to: 3, label: "" },
        { from: 1, to: 4, label: "" },
        { from: 1, to: 5, label: "" },
        { from: 1, to: 6, label: "" },
        { from: 1, to: 7, label: "" },

        { from: 2, to: 201, label: "" },
        { from: 2, to: 202, label: "" },
        { from: 2, to: 203, label: "" },
        { from: 2, to: 204, label: "" },
        { from: 2, to: 205, label: "" },
        { from: 2, to: 206, label: "" },
        { from: 2, to: 207, label: "" },

        { from: 202, to: 20201, label: "" },
        { from: 202, to: 20202, label: "" },
        { from: 20202, to: 2020201, label: "" },
        { from: 20202, to: 2020202, label: "" },
        { from: 2020202, to: 202020201, label: "" },
        { from: 2020202, to: 202020202, label: "" },

        { from: 203, to: 20301, label: "" },
        { from: 203, to: 20302, label: "" },
        { from: 20302, to: 2030201, label: "" },
        { from: 2030201, to: 203020101, label: "" },
        { from: 2030201, to: 203020102, label: "" },
        { from: 2030201, to: 203020103, label: "" },


        { from: 3, to: 301, label: "" },
        { from: 3, to: 302, label: "" },
        { from: 3, to: 303, label: "" },
        { from: 3, to: 304, label: "" },
        { from: 3, to: 305, label: "" },
        { from: 3, to: 306, label: "" },
        { from: 3, to: 307, label: "" },

        { from: 302, to: 30201, label: "" },
        { from: 302, to: 30202, label: "" },
        { from: 302, to: 30203, label: "" },
        { from: 30202, to: 3020201, label: "" },
        { from: 3020201, to: 302020101, label: "" },
        { from: 3020201, to: 302020102, label: "" },

        { from: 4, to: 401, label: "" },
        { from: 4, to: 402, label: "" },
        { from: 4, to: 403, label: "" },
        { from: 4, to: 404, label: "" },
        { from: 4, to: 405, label: "" },
        { from: 4, to: 406, label: "" },
        { from: 4, to: 407, label: "" },

        { from: 5, to: 501, label: "" },
        { from: 5, to: 502, label: "" },
        { from: 5, to: 503, label: "" },
        { from: 5, to: 504, label: "" },
        { from: 5, to: 505, label: "" },
        { from: 5, to: 506, label: "" },
        { from: 5, to: 507, label: "" },

        { from: 6, to: 601, label: "" },
        { from: 6, to: 602, label: "" },

        { from: 7, to: 701, label: "" },
        { from: 7, to: 702, label: "" },
        { from: 7, to: 703, label: "" },

        // 共同类别
        { from: 205, to: 901, label: "" },
        { from: 301, to: 901, label: "" },
        { from: 506, to: 902, label: "" },
        { from: 20301, to: 902, label: "" },
        { from: 2020201, to: 902, label: "" },
        { from: 301, to: 903, label: "" },
        { from: 404, to: 903, label: "" },

        // 同业务联系
        { from: 303, to: 305, label: "" },
        { from: 301, to: 304, label: "" },

        // 跨域关系
        { from: 204, to: 505, label: "" },
        { from: 206, to: 701, label: "" },
        { from: 302, to: 507, label: "" },
        { from: 401, to: 502, label: "" },
        { from: 501, to: 601, label: "" },
        { from: 207, to: 402, label: "" },
        { from: 203, to: 204, label: "" },
        { from: 407, to: 504, label: "" },
    ]);

    // 初始化图谱
    const network = initGraph("network-home", nodes, edges);
    if (!network) {
        console.error("图谱初始化失败");
        return;
    }

    // 跳转到洞察页面
    document.getElementById('start-insight-btn').addEventListener('click', () => {
        window.location.href = 'insight.html'; // 跳转到洞察页面
    });

    // 边栏伸缩
    document.getElementById('toggle-sidebar').addEventListener('click', () => {
        const sidebar = document.getElementById('sidebar');
        sidebar.classList.toggle('collapsed');
    });

    // 切换数据图谱和资产概览
    const dataGraphBtn = document.getElementById('data-graph-btn');
    const assetOverviewBtn = document.getElementById('asset-overview-btn');
    const networkHome = document.getElementById('network-home');
    const assetOverview = document.getElementById('asset-overview');

    dataGraphBtn.addEventListener('click', () => {
        dataGraphBtn.classList.add('active');
        assetOverviewBtn.classList.remove('active');
        networkHome.style.display = 'block';
        assetOverview.classList.add('hidden');
    });

    assetOverviewBtn.addEventListener('click', () => {
        assetOverviewBtn.classList.add('active');
        dataGraphBtn.classList.remove('active');
        networkHome.style.display = 'none';
        assetOverview.classList.remove('hidden');
        initCharts(); // 初始化图表
    });

    // 初始化图表
    function initCharts() {
        // 饼图
        const pieChart = echarts.init(document.getElementById('pie-chart'));
        pieChart.setOption({
            title: { text: '资产分布', left: 'center' },
            tooltip: { trigger: 'item' },
            series: [{
                name: '资产分布',
                type: 'pie',
                radius: '50%',
                data: [
                    { value: 40, name: '家庭用户' },
                    { value: 30, name: '企业用户' },
                    { value: 20, name: '高价值用户' },
                    { value: 10, name: '新用户' }
                ],
                emphasis: { itemStyle: { shadowBlur: 10, shadowOffsetX: 0, shadowColor: 'rgba(0, 0, 0, 0.5)' } }
            }]
        });

        // 柱状图
        const barChart = echarts.init(document.getElementById('bar-chart'));
        barChart.setOption({
            title: { text: '用户增长趋势', left: 'center' },
            tooltip: { trigger: 'axis' },
            xAxis: { type: 'category', data: ['1月', '2月', '3月', '4月', '5月', '6月'] },
            yAxis: { type: 'value' },
            series: [{
                name: '用户增长',
                type: 'bar',
                data: [120, 200, 150, 80, 70, 110],
                itemStyle: { color: '#4169E1' }
            }]
        });

        // 折线图
        const lineChart = echarts.init(document.getElementById('line-chart'));
        lineChart.setOption({
            title: { text: '用户活跃度', left: 'center' },
            tooltip: { trigger: 'axis' },
            xAxis: { type: 'category', data: ['1月', '2月', '3月', '4月', '5月', '6月'] },
            yAxis: { type: 'value' },
            series: [{
                name: '活跃度',
                type: 'line',
                data: [80, 120, 90, 150, 200, 180],
                itemStyle: { color: '#32CD32' }
            }]
        });

        // 热力图
        const heatmap = echarts.init(document.getElementById('heatmap'));
        heatmap.setOption({
            title: { text: '用户行为热力图', left: 'center' },
            tooltip: { position: 'top' },
            xAxis: { type: 'category', data: ['周一', '周二', '周三', '周四', '周五', '周六', '周日'] },
            yAxis: { type: 'category', data: ['0-6', '6-12', '12-18', '18-24'] },
            visualMap: { min: 0, max: 10, calculable: true, orient: 'horizontal', left: 'center', bottom: '10%' },
            series: [{
                name: '行为热度',
                type: 'heatmap',
                data: [
                    [0, 0, 5], [0, 1, 7], [0, 2, 3], [0, 3, 8],
                    [1, 0, 6], [1, 1, 9], [1, 2, 4], [1, 3, 7],
                    [2, 0, 8], [2, 1, 6], [2, 2, 5], [2, 3, 9],
                    [3, 0, 7], [3, 1, 8], [3, 2, 6], [3, 3, 5],
                    [4, 0, 9], [4, 1, 7], [4, 2, 8], [4, 3, 6],
                    [5, 0, 6], [5, 1, 5], [5, 2, 7], [5, 3, 8],
                    [6, 0, 8], [6, 1, 9], [6, 2, 6], [6, 3, 7]
                ],
                label: { show: true, color: '#000' }
            }]
        });
    }
});

// 筛选条件展开/隐藏
function toggleFilterGroup(element) {
    const filterGroup = element.parentElement;
    filterGroup.classList.toggle('expanded');
}

